/**
 * <p>Title: CommonServiceImpl.java</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2015</p>
 * <p>Company: SiySoft</p>
 * @author liguanghui
 * @date 2015年5月28日
 * @version 1.0
 */
package com.siysoft.tc.base.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.siysoft.exception.FileUploadException;
import com.siysoft.exception.MailPropertyNotFoundException;
import com.siysoft.exception.SMSCodeSendFailedException;
import com.siysoft.tc.base.bean.to.Banner;
import com.siysoft.tc.base.bean.to.ClassType;
import com.siysoft.tc.base.bean.to.Resource;
import com.siysoft.tc.base.bean.to.VerifyCode;
import com.siysoft.tc.base.service.ICommonService;
import com.siysoft.tc.base.service.IVerifyCodeService;
import com.siysoft.util.COMMON;
import com.siysoft.util.RandomCode;
import com.siysoft.util.fileupload.FileUpload;
import com.siysoft.util.mail.MailSenderInfo;
import com.siysoft.util.mail.SimpleMailSender;
import com.siysoft.util.sms.SMSSender;
import com.tc.base.dao.BaseDAO;

/**
 * <p>
 * Title: CommonServiceImpl
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Company: SiySoft
 * </p>
 * 
 * @author vitoryu
 * @date 2015年9月15日
 */
@Service
public class CommonServiceImpl implements ICommonService {

	@Autowired
	IVerifyCodeService verifyCodeService;
	@Autowired
	BaseDAO baseDao;
	@Autowired
	MailSenderInfo mailSenderInfo;

	/*
	 * （非 Javadoc） <p>Title: sendEmailVerfiyCode</p> <p>Description: </p>
	 * 
	 * @param toAddress
	 * 
	 * @see com.siysoft.yihaoxue.base.service.CommonService#sendEmailVerfiyCode
	 * (java.lang.String)
	 */
	@Override
	public boolean sendEmailVerfiyCode(String toAddress, String requestIp)
			throws MailPropertyNotFoundException, MessagingException {


		VerifyCode code = new VerifyCode(RandomCode.getSixDigitRandomCode(),
				"EMAIL", toAddress, requestIp);

		mailSenderInfo.setToAddress(toAddress);
		mailSenderInfo.setSubject("验证码测试");
		mailSenderInfo.setContent("您的验证码是" + code.getCodeValue());
		// 把验证码插入到数据库。
		SimpleMailSender sms = new SimpleMailSender();

		sms.sendTextMail(mailSenderInfo);
		// 如果发送成功，插入验证码到数据库
		verifyCodeService.insertVerifyCode(code);
		return true;

	}
	
	@Override
	public boolean sendEmailMsg(String toAddress, String requestIp,String title,String content)
			throws MailPropertyNotFoundException, MessagingException{
		System.out.println("****************************222222222222222222222");
		mailSenderInfo.setToAddress(toAddress);
		mailSenderInfo.setSubject(title);
		mailSenderInfo.setContent(content);
		// 把验证码插入到数据库。
		SimpleMailSender sms = new SimpleMailSender();
		System.out.println("****************************3333333333333333333333");
		System.out.println("****************************getUserName" +mailSenderInfo.getUserName());
		System.out.println("****************************getMailServerHost" +mailSenderInfo.getMailServerHost());
		System.out.println("****************************getMailServerPort" +mailSenderInfo.getMailServerPort());
		sms.sendTextMail(mailSenderInfo);
		System.out.println("****************************44444444444444444444");
		/*// 如果发送成功，插入邮件发送记录+
		verifyCodeService.insertEmail(content);*/
		return true;

	}

	/*
	 * （非 Javadoc） <p>Title: sendSMSVerifyCode</p> <p>Description: </p>
	 * 
	 * @param phoneNumber
	 * 
	 * @param requestIp
	 * 
	 * @see
	 * com.siysoft.yihaoxue.base.service.CommonService#sendSMSVerifyCode(java
	 * .lang.String, java.lang.String)
	 */
	@Override
	public boolean sendSMSVerifyCode(String phoneNumber, String requestIp)
			throws SMSCodeSendFailedException {
		SMSSender sender = null;
		boolean result = true;
		// 生成验证码实体
		VerifyCode code = new VerifyCode(RandomCode.getSixDigitRandomCode(),
				"MOBILE", phoneNumber, requestIp);

		String smsContent = "【艺好学】您的验证码是" + code.getCodeValue()
				+ "，在10分钟内有效。如非本人操作请忽略本短信。";
		try {
			sender = new SMSSender();
			sender.sendSMS(phoneNumber, smsContent);
			verifyCodeService.insertVerifyCode(code);
		} catch (Exception e) {
			e.printStackTrace();
			result = false;
		}
		return result;
	}

	/*
	 * （非 Javadoc） <p>Title: sendSMSVerifyCode</p> <p>Description: </p>
	 * 
	 * @param phoneNumber
	 * 
	 * @param requestIp
	 * 
	 * @see
	 * com.siysoft.yihaoxue.base.service.CommonService#sendSMSVerifyCode(java
	 * .lang.String, java.lang.String)
	 */
	@Override
	public boolean sendMsg(String phoneNumber, String msg)
			throws SMSCodeSendFailedException {
		SMSSender sender = null;
		boolean result = true;
		String smsContent = msg;
		try {
			sender = new SMSSender();
			sender.sendSMS(phoneNumber, smsContent);
		} catch (Exception e) {
			e.printStackTrace();
			result = false;
		}
		return result;
	}
	
	
	
	/*
	 * （非 Javadoc） <p>Title: fileUpload</p> <p>Description: </p>
	 * 
	 * @param request
	 * 
	 * @param response
	 * 
	 * @param upFolderName
	 * 
	 * @param upFileName
	 * 
	 * @param userId
	 * 
	 * @see com.siysoft.yihaoxue.base.service.CommonService#fileUpload(
	 * javax.servlet.http.HttpServletRequest,
	 * java.lang.String,
	 * java.lang.String,
	 * java.lang.String)
	 */
	@Override
	public Map<String, String> fileUpload(HttpServletRequest request, String userId)
			throws FileUploadException {
		Map<String, String> serviceFileList = new HashMap<String, String>();
		
	    MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;    
	    Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();  
	    //文件存放路径
	    String local = request.getServletContext().getRealPath("/"); 
	    System.out.println(local); 
	    //文件上传
	    FileUpload fileUpload = new FileUpload();   
	    serviceFileList = fileUpload.uploadFile(multipartRequest, fileMap, local, userId);
	    
	    return serviceFileList;  
	}

	
	
	public String getResourceUrl(String resType, int targetId) {
		String sql="from Resource where dataType='"+resType+"' and userId="+targetId;
		List<Resource> resource=baseDao.findByHQL(sql);
		if(resource.size()==0){
			return null;
		}
		return COMMON.home_url + resource.get(0).getImgPath();
		
	}
	
	
	@Override
	public List<Banner> getBanners(int limit) {
		String hql=" from Banner b order by b.bannerId desc";
		System.out.println(hql);
		return baseDao.findPageByHql(hql,0,limit);
	}

	@Override
	public List<ClassType> getClassType() throws Exception {
		String hql=" from ClassType ";
		return baseDao.findByHQL(hql);
	}
	


	
}
